package hoang.android.app.network;

import hoang.android.app.R;
import hoang.android.app.info.WebServiceConfig;
import hoang.android.app.ui.BaseActivity;

import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.util.Log;

public class AsyncHttpResponseProcess implements AsyncHttpResponseListener {

	private BaseActivity baseActivity; // de goi duoc cac phuong thuc ma viet
										// tai lop nay

	private boolean isShowDialog = true; // de show dialog (load data), ko show
											// dialog (dong bo data)

	// phuong thuc khoi tao
	public AsyncHttpResponseProcess(BaseActivity activity) {
		this.baseActivity = activity;
	}

	public AsyncHttpResponseProcess(BaseActivity activity, boolean isShow) {
		this.baseActivity = activity;
		this.isShowDialog = isShow;
	}

	@Override
	public void before() {
		// truoc khi load du lieu se xu ly o day
		// neu load data thi hien thi dialog
		// neu dong bo khong hien thi dialog
		if (isShowDialog) {
			// se show dialog
			baseActivity.showProgressDialog(baseActivity.getString(R.string.waiting));
		}
	}

	@Override
	public void after(int statusCode, HttpResponse response) {
		// day la phuong thuc duoc goi khi qua trinh load data ket thuc
		if (isShowDialog) {
			// tai day se dong dialog
			baseActivity.hideProgressDialog();
		}

		// tai day se xu ly ket qua tra ve dua vao statusCode
		// (thanh cong thi xu ly du lieu, khong thanh cong thi phai thong bao
		// cho nguoi dung loi gi)
		switch (statusCode) {
		case AsyncHttpBase.STATUS_NETWORK_OFF:
			// show dialog thong bao network khong co
			baseActivity.showPositiveCustomDialog(baseActivity, 
					R.string.error, 
					R.string.network_disconnect, 
					R.drawable.button_ok);
			break;

		case AsyncHttpBase.STATUS_NETWORK_ERROR:
			// show dialog thong bao networ loi
			baseActivity.showPositiveCustomDialog(baseActivity, 
					R.string.error, 
					R.string.network_error, 
					R.drawable.button_ok);
			break;
		case AsyncHttpBase.STATUS_NETWORK_EXCEIPTION:
			// show dialog thong bao network exceiption xay ra
			baseActivity.showPositiveCustomDialog(baseActivity, 
					R.string.error, 
					R.string.network_error, 
					R.drawable.button_ok);
			break;
		case AsyncHttpBase.STATUS_NETWORK_TIMEOUT:
			// show dialog thong bao network timeout
			baseActivity.showPositiveCustomDialog(baseActivity, 
					R.string.error, 
					R.string.network_timeout, 
					R.drawable.button_ok);
			break;
		case AsyncHttpBase.STATUS_NETWORK_OK:
			// day la trang nhan response thanh cong
			// xu ly xem response nhu the nao
			processHttpResponse(response);
			break;
		default:
			break;
		}

	}

	/**
	 * ham xu ly response thanh cong lay json, xu ly json
	 * 
	 * @param response
	 */
	private void processHttpResponse(HttpResponse response) {
		//tai day dua vao response se lay du lieu kieu string
		String jsonString = "";
		try {
			jsonString = EntityUtils.toString(response.getEntity());
			if(jsonString==null){
				//thong bao loi du lieu
				baseActivity.showPositiveCustomDialog(baseActivity, 
						R.string.error, 
						R.string.json_parser_error, 
						R.drawable.button_ok);
				return;
			}else{
				//bat dau xu ly du lieu
				int success = 0; //that bai, 1: thanh cong
				
				JSONObject jsonObject = new JSONObject(jsonString);
				
				try {
					success = jsonObject.getInt(WebServiceConfig.PARAM_SUCCESS);
					if(success==1){
						//thanh cong
						processIfResponseSuccess(jsonObject);
					}else{
						//that bai
						processIfResponseFail(jsonObject);
					}
				} catch (Exception e) {
					processIfResponseSuccess(jsonObject);
				}
				
			}
		} catch (Exception e) {
			Log.e("TEST", e.toString());
		}
	}
	
	//xay dung phuong thuc de xu ly du lieu thanh cong
	@SuppressLint("ShowToast")
	public void processIfResponseSuccess(JSONObject json){
		
	}
	
	//xay dung phuong thuc de xu ly du lieu khi that bai
	public void processIfResponseFail(JSONObject json) {
		try {

			int status = json.getInt("status");
			if (status != 200) {

				String message = json.getString("message");
				baseActivity.showPositiveCustomDialog(baseActivity, "Error",
						message, R.drawable.button_ok);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}
	
}
